//
//  SendRepairVC.m
//  CJRailTransport
//
//  Created by Hank on 2018/1/22.
//  Copyright © 2018年 amplity. All rights reserved.
//
#define SendLocationTitle          @"请选择送修地点"
#define SendTimeTitle              @"请选择送修日期"
#define CarriersTitle              @"请选择承运方"
#define DamagedTitle               @"请选择受损程度"

#import "SendRepairVC.h"
#import "SendRepairChooseVC.h"
#import "SendRepairCell.h"
#import "STPickerSingle.h"
#import "STPickerDate.h"
#import "UIButton+Expand.h"
#import "UnloadingAndHangBoxShowListVC.h"

@interface SendRepairVC ()<STPickerDateDelegate,STPickerSingleDelegate>
// 选择集装箱数组
@property (strong, nonatomic) NSMutableArray *containerDataArray;

@end

@implementation SendRepairVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 设置圆角
    [self setRadius];
    // 注册Cell
    [self registerCells];
    // 设置BarButtonItem
    [self setBarButtonItem];
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = @"维修";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[SendRepairCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[SendRepairCell reuseIdentifier]];
}

#pragma mark - Radius
// 设置圆角
- (void)setRadius
{
    [self.damagedBtn radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.sendLocation radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.sendTime radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.carriers radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
}

#pragma mark - initArray
// 初始化数组
- (NSMutableArray *)containerDataArray
{
    if (!_containerDataArray) {
        _containerDataArray = [[NSMutableArray alloc]init];
    }
    return _containerDataArray;
}

// 设置BarButtonItem
- (void)setBarButtonItem
{
    UIButton *itemBtn = [UIButton setLeftOrRightButton:nil direction:@"right" title:@"确定"];
    [itemBtn addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:itemBtn];
}

#pragma mark - Picker
// 显示日期选择器
- (void)showDatePicker
{
    STPickerDate *datePicker = [[STPickerDate alloc]init];
    [datePicker setDelegate:self];
    [datePicker setYearLeast:2000];
    [datePicker setYearSum:50];
    [datePicker setContentMode:STPickerContentModeBottom];
    [datePicker show];
}
// 显示内容选择器
- (void)showContentPicker:(NSMutableArray *)dataArray pickerTitle:(NSString *)pickerTitle
{
    // 创建内容选择器
    STPickerSingle *pickerSingle = [[STPickerSingle alloc]init];
    // 添加数据
    [pickerSingle setArrayData:dataArray];
    // 添加标题
    [pickerSingle setTitle:pickerTitle];
    [pickerSingle setContentMode:STPickerContentModeBottom];
    [pickerSingle setDelegate:self];
    // 显示内容选择器
    [pickerSingle show];
}

// 确定 Action
- (void)rightItemAction
{
    UnloadingAndHangBoxShowListVC *unloadingAndHangBoxShowListVC = [[UnloadingAndHangBoxShowListVC alloc]init];
    unloadingAndHangBoxShowListVC.containerDataArray = _containerDataArray;
    [self.navigationController pushViewController:unloadingAndHangBoxShowListVC animated:YES];
}

#pragma mark - Action 
// 送修地点 Action
- (IBAction)sendLocationAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"送修地点测试1",@"送修地点测试2",@"送修地点测试3",@"送修地点测试4",@"送修地点测试5",@"送修地点测试6"]] pickerTitle:SendLocationTitle];
}
// 送修日期 Action
- (IBAction)sendTimeAction:(UIButton *)sender
{
    // 显示日期选择器
    [self showDatePicker];
}
// 承运商 Action
- (IBAction)carriersAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"承运商测试1",@"承运商测试2",@"承运商测试3",@"承运商测试4",@"承运商测试5",@"承运商测试6"]] pickerTitle:CarriersTitle];
}
// 受损程度 Action
- (IBAction)damagedAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"受损程度测试1",@"受损程度测试2",@"受损程度测试3",@"受损程度测试4",@"受损程度测试5",@"受损程度测试6"]] pickerTitle:DamagedTitle];
}

#pragma mark - pickerDateDelegate
// date
-(void)pickerDate:(STPickerDate *)pickerDate year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    // 更新日期
    [self.sendTime setTitle:[NSString stringWithFormat:@"  %ld-%@-%@", year, [self digitJudge:month], [self digitJudge:day]] forState:UIControlStateNormal];
}
// 数字位数判断
- (NSString *)digitJudge:(NSInteger)number
{
    // 转换的字符串
    NSString *numberStr;
    
    if (number < 10) {
        numberStr = [NSString stringWithFormat:@"0%ld",number];
    }else{
        numberStr = [NSString stringWithFormat:@"%ld",number];
    }
    return numberStr;
}
// content
- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle
{
    if ([pickerSingle.title isEqualToString:SendLocationTitle]) {
        [self.sendLocation setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:SendTimeTitle]) {
        [self.sendTime setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:CarriersTitle]) {
        [self.carriers setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:DamagedTitle]) {
        // 修改按钮内容
        [self.damagedBtn setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
        // 跳转到送修选择页面
        SendRepairChooseVC *sendRepairChooseVC = [[SendRepairChooseVC alloc]init];
        // 测试使用的数据判断
        if ([selectedTitle isEqualToString:@"受损程度测试1"]) {
            sendRepairChooseVC.type = @"1";
        }else{
            sendRepairChooseVC.type = @"2";
        }
        sendRepairChooseVC.containerDataArray = [NSMutableArray arrayWithArray:self.containerDataArray];
        // 防止循环引用
        WEAK_REF(self);
        // 复选框点击 Block
        [sendRepairChooseVC setCheckImageClickBlock:^(NSString *content, BOOL state){
            //======修改Model 自定义字段======
            if (state) {
                [weak_self.containerDataArray addObject:content];
            }else{
                // 移除指定元素
                [weak_self.containerDataArray removeObject:content];
            }
            // 刷新Cell
            [weak_self.myTableView reloadData];
        }];
        // 全选复选框点击 Block
        [sendRepairChooseVC setAllCheckImageClickBlock:^(NSMutableArray *dataArray, BOOL state){
            // 遍历数据数组
            for (int i = 0 ; i < dataArray.count; i++) {
                // 查询数组中是否保存集装箱编号
                BOOL isbool = [weak_self.containerDataArray containsObject:dataArray[i]];
                if (state) {
                    // 全选
                    if (!isbool) {
                        [weak_self.containerDataArray addObject:dataArray[i]];
                    }
                }else{
                    // 取消全选
                    if (isbool) {
                        [weak_self.containerDataArray removeObject:dataArray[i]];
                    }
                }
            }
            // 刷新Cell
            [weak_self.myTableView reloadData];
        }];
        [self.navigationController pushViewController:sendRepairChooseVC animated:YES];
    }
}

#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.containerDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SendRepairCell *cell = [tableView dequeueReusableCellWithIdentifier:[SendRepairCell reuseIdentifier] forIndexPath:indexPath];
    cell.indexPath = indexPath;
    [cell setData:[NSString stringWithFormat:@"%@",self.containerDataArray[indexPath.row]]];
    return cell;
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 移除数据
        [_containerDataArray removeObjectAtIndex:indexPath.row];
        // 移除单元格
        //        [_myTableView reloadData];
        [_myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

// 设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
